<?php
defined('IN_SEC') or exit('Access Denied.');
defined('COMPILED_PATH') or define('COMPILED_PATH', APP_PATH . '/include/compiled/');
defined('TEMPLATE_PATH') or define('TEMPLATE_PATH', APP_PATH . '/include/template/');

function __parsecall($matches) {
	return '<?php include template("'.$matches[1].'"); ?>';
}

function __parse($template, $compiled) {

    if(!($content = file_get_contents($template))){
		return false;
	}
	$header = "<?php\nif ( !defined('IN_SEC') ){exit();}\n?>\n";

	$content = preg_replace( '/^(\xef\xbb\xbf)/', '', $content); //EFBBBF   
    $content = preg_replace("/\<\!\-\-\s*\\\$\{(.+?)\}\s*\-\-\>/ies", "__replace('<?php \\1; ?>')", $content);
    $content = preg_replace("/\{(\\\$[a-zA-Z0-9_\[\]\\\ \-\'\,\%\*\/\.\(\)\>\'\"\$\x7f-\xff]+)\}/s", "<?php echo \\1; ?>", $content);
    $content = preg_replace("/\\\$\{(.+?)\}/ies", "__replace('<?php echo \\1; ?>')", $content);

    $content = preg_replace("/\<\!\-\-\s*\{else\s*if\s+(.+?)\}\s*\-\-\>/ies", "__replace('<?php } else if(\\1) { ?>')", $content);
    $content = preg_replace("/\<\!\-\-\s*\{elif\s+(.+?)\}\s*\-\-\>/ies", "__replace('<?php } else if(\\1) { ?>')", $content);
    $content = preg_replace("/\<\!\-\-\s*\{else\}\s*\-\-\>/is", "<?php } else { ?>", $content);

    for($i = 0; $i < 5; ++$i) {
        $content = preg_replace("/\<\!\-\-\s*\{loop\s+(\S+)\s+(\S+)\s+(\S+)\s*\}\s*\-\-\>(.+?)\<\!\-\-\s*\{\/loop\}\s*\-\-\>/ies", "__replace('<?php if(is_array(\\1)){foreach(\\1 AS \\2=>\\3) { ?>\\4<?php }}?>')", $content);
        $content = preg_replace("/\<\!\-\-\s*\{loop\s+(\S+)\s+(\S+)\s*\}\s*\-\-\>(.+?)\<\!\-\-\s*\{\/loop\}\s*\-\-\>/ies", "__replace('<?php if(is_array(\\1)){foreach(\\1 AS \\2) { ?>\\3<?php }}?>')", $content);
        $content = preg_replace("/\<\!\-\-\s*\{if\s+(.+?)\}\s*\-\-\>(.+?)\<\!\-\-\s*\{\/if\}\s*\-\-\>/ies", "__replace('<?php if(\\1){?>\\2<?php }?>')", $content);
    }

    //Add for call <!--{include othertpl}-->
    $content = preg_replace("#<!--\s*{\s*include\s+([^\{\}]+)\s*\}\s*-->#i", '<?php include template("\\1");?>', $content);
	/*$content = preg_replace("#<!--\s*{\s*([^\{\}]+)\s+([^\{\}]+)\s*\}\s*-->#i", '<?php include template("\\2", "\\1");?>', $content);*/

	if ( !is_dir(dirname($compiled)) ){
		__compiled_dir(dirname($compiled));
	}

	$content = $header.$content;
    //Add value namespace
    if(!file_put_contents($compiled, $content)){
        return false;
	}

    return true;
}

function __replace($string) {
    return str_replace('\"', '"', $string);
}

function __compiled_dir($dir){
	return dmkdir($dir);
}

function __template($tmpname) {

	$filename = preg_replace( '/\.html$/', '', $tmpname);
	$filepath = TEMPLATE_PATH . $filename . '.html';
	$compiled = COMPILED_PATH . str_replace('/', '_', $filename) . '.$#tmp#$.php';

	if ( !file_exists($filepath) ){
		if ( strpos($filename, 'ajax_') ){
			$filename = str_replace('/ajax_', '/', $filename);
			return __template($filename);
		}
		$bug = new Bug;
		$bug->template($filename, $filepath);
	}
	if(file_exists($compiled) === false|| @filemtime($filepath) != @filemtime($compiled)) {
		__parse($filepath, $compiled);
	}
	return $compiled;
}
?>